package cn.iocoder.yudao.module.member.service.studentShow;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.studentShow.vo.StudentShowCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.studentShow.vo.StudentShowExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.studentShow.vo.StudentShowPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.studentShow.vo.StudentShowUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.studentShow.StudentShowConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.studentShow.StudentShowDO;
import cn.iocoder.yudao.module.member.dal.mysql.studentShow.StudentShowMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.STUDENT_SHOW_NOT_EXISTS;

/**
 * 学生风采 Service 实现类
 *
 * @author 谭万全
 */
@Service
@Validated
public class StudentShowServiceImpl implements StudentShowService {

    @Resource
    private StudentShowMapper studentShowMapper;

    @Override
    public Long createStudentShow(StudentShowCreateReqVO createReqVO) {
        // 插入
        StudentShowDO studentShow = StudentShowConvert.INSTANCE.convert(createReqVO);
        studentShow.setCreator(SecurityFrameworkUtils.getLoginUserId().toString());
        studentShow.setUpdater(SecurityFrameworkUtils.getLoginUserId().toString());
        studentShowMapper.insert(studentShow);
        // 返回
        return studentShow.getId();
    }

    @Override
    public void updateStudentShow(StudentShowUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateStudentShowExists(updateReqVO.getId());
        // 更新
        StudentShowDO updateObj = StudentShowConvert.INSTANCE.convert(updateReqVO);
        updateObj.setUpdater(SecurityFrameworkUtils.getLoginUserId().toString());
        studentShowMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudentShow(Long id) {
        // 校验存在
        this.validateStudentShowExists(id);
        // 删除
        StudentShowDO updateObj=new StudentShowDO();
        updateObj.setUpdater(WebFrameworkUtils.getLoginUserName());
        updateObj.setUpdateTime(new Date());
        updateObj.setId(id);
        studentShowMapper.updateById(updateObj);
        studentShowMapper.deleteById(id);
    }

    private void validateStudentShowExists(Long id) {
        if (studentShowMapper.selectById(id) == null) {
            throw exception(STUDENT_SHOW_NOT_EXISTS);
        }
    }

    @Override
    public StudentShowDO getStudentShow(Long id) {
        return studentShowMapper.selectById(id);
    }

    @Override
    public List<StudentShowDO> getStudentShowList(Collection<Long> ids) {
        return studentShowMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudentShowDO> getStudentShowPage(StudentShowPageReqVO pageReqVO) {
        return studentShowMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudentShowDO> getStudentShowList(StudentShowExportReqVO exportReqVO) {
        return studentShowMapper.selectList(exportReqVO);
    }

    @Override
    public PageResult<StudentShowDO> recoveryStudentShowPage(StudentShowPageReqVO pageReqVO) {
        // MyBatis Plus 查询
        IPage<StudentShowDO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        // 转换返回
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(pageReqVO.getStatus())){
            params.put("status",pageReqVO.getStatus());
        }
        if(StringUtils.isNotBlank(pageReqVO.getTitle())){
            params.put("title",pageReqVO.getTitle());
        }
        if(StringUtils.isNotBlank(pageReqVO.getCreator())){
            params.put("creator",pageReqVO.getCreator());
        }
        mpPage.setRecords(studentShowMapper.recoveryStudentShowPage(mpPage, params));
        return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
    }

    @Override
    public StudentShowDO getRecoveryStudentShowById(Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id",id);
        List<StudentShowDO> list = studentShowMapper.recoveryStudentShowPage(null, params);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void updateBatchRecoveryStudentShow(List<Long> ids) {
        studentShowMapper.updateBatchRecoveryStudentShow(ids);
    }
}
